package com.solo.servlet;

import com.solo.entity.Article;
import com.solo.entity.Category;
import com.solo.entity.User;
import com.solo.service.ArticleService;
import com.solo.service.CategoryService;
import com.solo.service.UserService;
import com.solo.service.impl.ArticleServiceImpl;
import com.solo.service.impl.CategoryServiceImpl;
import com.solo.service.impl.UserServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "ArticleServlet", value = "/ArticleServlet")
public class ArticleServlet extends HttpServlet {
    private ArticleService articleService;
    private UserService userService;
    private CategoryService categoryService;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //获取对应的请求参数
        String action = request.getParameter("method");
        System.out.println("action====" + action);
        try {
            switch (action) {
                case "new":
                    showNewForm(request, response);
                    break;
                case "add":
                    addArticle(request, response);
                    break;
                case "delete":
                    deleteArticle(request, response);
                    break;
                case "edit":
                    showEditForm(request, response);
                    break;
                case "detail":
                    showDetailForm(request, response);
                    break;
                case "update":
                    updateArticle(request, response);
                    break;
                default:
                    listArticle(request, response);
                    break;
            }
        } catch (Exception ex) {
            throw new ServletException(ex);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        doGet(request, response);
    }

    private void showNewForm(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        userService = new UserServiceImpl();
        List<User> userList = userService.selectAllUser();
        request.setAttribute("userList", userList);
        categoryService = new CategoryServiceImpl();
        List<Category> listCategory = categoryService.selectAllCategory();
        request.setAttribute("categoryList", listCategory);
        RequestDispatcher dispatcher = request.getRequestDispatcher("views/article/addArticle.jsp");
        dispatcher.forward(request, response);
    }


    private void addArticle(HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        String title = request.getParameter("title");
        String content = request.getParameter("content");

        System.out.println("title====" + title);
        System.out.println("content====" + content);

        Article article = new Article(title, content);
        articleService = new ArticleServiceImpl();
        articleService.addArticle(article);
        // response.sendRedirect("user.jsp");
        listArticle(request, response);
    }

    private void deleteArticle(HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        System.out.println("deleteArticle====");

        Long id = Long.parseLong(request.getParameter("id"));
        System.out.println("id====" + id);
        Article article = new Article(id);
        articleService = new ArticleServiceImpl();
        articleService.deleteArticle(article);
        listArticle(request, response);
    }

    private void showEditForm(HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        Long id = Long.parseLong(request.getParameter("id"));
        System.out.println("id====" + id);
        articleService = new ArticleServiceImpl();
        Article article = articleService.selectArticleById(id);
        RequestDispatcher dispatcher = request.getRequestDispatcher("views/article/editArticle.jsp");
        request.setAttribute("article", article);
        dispatcher.forward(request, response);
    }
    private void updateArticle(HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        Long id = Long.parseLong(request.getParameter("id"));
        String title = request.getParameter("title");
        String content = request.getParameter("content");

        Article article = new Article(id, title, content);

        articleService = new ArticleServiceImpl();
        articleService.updateArticle(article);
        listArticle(request, response);
    }

    private void listArticle(HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        // response.setContentType("text/html;charset=utf-8");
        userService = new UserServiceImpl();
        List<User> userList = userService.selectAllUser();
        request.setAttribute("userList", userList);
        categoryService = new CategoryServiceImpl();
        List<Category> listCategory = categoryService.selectAllCategory();
        request.setAttribute("categoryList", listCategory);
        articleService = new ArticleServiceImpl();
        List<Article> listArticle = articleService.selectAllArticle();
        request.setAttribute("articleList", listArticle);
        RequestDispatcher dispatcher = request.getRequestDispatcher("views/article/article.jsp");
        dispatcher.forward(request, response);
    }

    private void showDetailForm(HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        Long id = Long.parseLong(request.getParameter("id"));
        System.out.println("id====" + id);
        // 查询所有用户，并将数据返回前端
        userService = new UserServiceImpl();
        List<User> userList = userService.selectAllUser();
        request.setAttribute("userList", userList);

        // 查询所有博客分类，并将数据返回前端
        categoryService = new CategoryServiceImpl();
        List<Category> listCategory = categoryService.selectAllCategory();
        request.setAttribute("categoryList", listCategory);

        articleService = new ArticleServiceImpl();
        Article article = articleService.selectArticleById(id);
        RequestDispatcher dispatcher = request.getRequestDispatcher("detail.jsp");
        request.setAttribute("article", article);
        dispatcher.forward(request, response);
    }
}